package com.jack.vipmis.vip.api.web.controller;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.member.entity.Member;
import com.jack.vipmis.vip.member.entity.Verify;
import com.jack.vipmis.vip.member.service.MemberService;
import com.jack.vipmis.vip.member.service.SendMsg;
import com.jack.vipmis.vip.member.service.VerifyService;

/**
 * 系统api接口
 * 
 * @author JackLiang
 * @date 2014-7-8
 * @version 1.0
 */
@Controller
@RequestMapping(value = "api")
public class SystemApiController extends BaseController<Member, Long> {

	@Autowired
	private MemberService	memberService;

	@Autowired
	private VerifyService	verifyService;

	@Autowired
	private SendMsg			sendMsg;

	/**
	 * 系统登陆
	 * 
	 * @param request
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> login(@RequestParam("phone") long phone, @RequestParam("pwd") String pwd) {
		try {
			Member m = memberService.login(phone, pwd);
			if (m != null) {
				return MsgUtil.doneOk(m);
			}
			return MsgUtil.doneMsg(MsgUtil.YZM_ERROR, "用户信息不存在", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 验证码
	 * 
	 * @param phone
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "verify")
	@ResponseBody
	private Map<String, Object> doVerify(@RequestParam("phone") Long phone) {
		try {
			int yzm = (int) ((Math.random() * 9 + 1) * 100000);
			Member m = memberService.findByPhone(phone);
			if (m != null) {
				return MsgUtil.doneMsg(MsgUtil.FAIL, "该用户已存在！", m);
			} else {
				Verify v = new Verify();
				v.setPhone(phone);
				v.setYzm(Integer.toString(yzm));
				Verify verify = verifyService.save(v);

				sendMsg.testSend(phone, yzm);

				return MsgUtil.doneMsg(MsgUtil.OK, "验证发送成功!", verify);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 注册
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> register(@RequestParam("uName") String uName, @RequestParam("pwd") String pwd,
			@RequestParam("phone") long phone, @RequestParam("sex") int sex, @RequestParam("yzm") String yzm) {
		Member m = new Member();
		m.setFirstName(uName);
		m.setPhone(phone);
		m.setPwd(pwd);
		m.setSex(sex);
		Verify verify = verifyService.findByPhone(phone);
		try {
			// 验证验证码
			if (StringUtils.isNotBlank(yzm) && verify != null && verify.getYzm().equals(yzm)) {
				// 验证电话号码
				Member member = memberService.findByPhone(phone);
				if (member != null) {
					return MsgUtil.doneMsg(MsgUtil.FAIL, "该账号已注册!", null);
				} else {
					memberService.save(m);
					return MsgUtil.doneOk(m);
				}
			} else {
				return MsgUtil.doneMsg(MsgUtil.YZM_ERROR, "验证码输入错误!", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 清空验证码信息
			verifyService.delete(verify);
		}
		return MsgUtil.doneFail();
	}
}
